/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tcpserver;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
/**
 *
 * @author Nguyễn Ngọc Anh - 0812004
 */
public class ClientConnect extends Thread{
        public Socket client;
	public Server server;

	private DataOutputStream dos;
	private DataInputStream dis;
	private boolean run;

        public ClientConnect(Server server, Socket client){
		try {
			this.server=server;
			this.client=client;
			dos= new DataOutputStream(client.getOutputStream());
			dis= new DataInputStream(client.getInputStream());
			run=true;
			this.start();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
    @Override
        public void run()
        {

            //các thao tác xử lý đối tượng ở đây
            int y = 3;

            do
            {
                //gửi 3 gói tin đến các client
                String out = "chia bai ne!, " ;
                sendMSG(out);

                String inp = getMSG();
                System.out.println(inp);
                y--;
            }
            while(y>0);

        }

        public void sendMSG(String data)
        {
		try {
			dos.writeUTF(data);
			dos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

       public String getMSG()
       {
		String data=null;
		try {
			data=dis.readUTF();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}
}



